package com.dianrui.fmi.fragment;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.SimulatedTransactionNewActivity;
import com.dianrui.fmi.adapter.SettlementAdapter;
import com.dianrui.fmi.bean.PositionBean;
import com.dianrui.fmi.event.RfreshSettlement;
import com.dianrui.fmi.event.RfreshSimulatedPage;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.dianrui.fmi.view.pulltoRefresh.PullToRefreshScrollView;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class FragmentSettlements extends BaseFragment {

    private String type;
    private SettlementAdapter  settlementAdapter;
    private List<PositionBean> mSettlementsList=new ArrayList<>();
    @BindView(R.id.settlement_list)
    ListView settlementList;
    @BindView(R.id.my_scrollview)
    PullToRefreshScrollView myScrollview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;


    @Override
    public int getContentViewId() {
        return R.layout.fragment_settmentles;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        settlementAdapter = new SettlementAdapter(getActivity(), mSettlementsList);
        settlementList.setAdapter(settlementAdapter);
        getSettlementList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshSettlement rfreshSettlement) {

    }


    /**
     * 页面切换
     */
    @Subscribe
    public  void onPageSelected(RfreshSimulatedPage event) {
        if (event != null) {
            SimulatedTransactionNewActivity.fragemntPosition = event.getType();
            type= event.getType();
            getSelectPageSettlementLists("3");

        }
    }





    private void getSettlementList() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", "3");
        jsonObject.addProperty("is_real", "2");
        OkHttpUtils.getInstance().postWithMemberToken(Url.SIMULATED_LIST, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                myScrollview.onRefreshComplete();
                                if (mSettlementsList != null) {
                                    mSettlementsList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        PositionBean positionBean = new PositionBean();
                                        positionBean.order_id = jsonArray.optJSONObject(i).optString("order_id");
                                        positionBean.createtime = jsonArray.optJSONObject(i).optString("create_time");
                                        positionBean.price = jsonArray.optJSONObject(i).optString("price");
                                        positionBean.end_time = jsonArray.optJSONObject(i).optString("end_time");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.earnings= jsonArray.optJSONObject(i).optString("earnings");
                                        positionBean.start_price = jsonArray.optJSONObject(i).optString("start_price");
                                        positionBean.full_symbol= jsonArray.optJSONObject(i).optString("full_symbol");
                                        positionBean.symbol= jsonArray.optJSONObject(i).optString("symbol");
                                        positionBean.name= jsonArray.optJSONObject(i).optString("name");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.service_charge = jsonArray.optJSONObject(i).optString("service_charge");
                                        positionBean.mix_price = jsonArray.optJSONObject(i).optString("mix_price");
                                        positionBean.direction= jsonArray.optJSONObject(i).optString("direction");
                                        positionBean.postion = jsonArray.optJSONObject(i).optString("overnight_fee");
                                        positionBean.margin = jsonArray.optJSONObject(i).optString("margin");
                                        positionBean.total = jsonArray.optJSONObject(i).optString("total");
                                        mSettlementsList.add(positionBean);
                                    }
                                    if (settlementAdapter != null) {
                                        settlementAdapter.refresh(mSettlementsList);
                                    }


                                }else {
                                    myScrollview.onRefreshComplete();
                                }
                            }else {
                                myScrollview.onRefreshComplete();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }

                        }
                        if (mSettlementsList.size() <= 0) {
                            emptyView.setVisibility(View.VISIBLE);
                            settlementList.setVisibility(View.GONE);
                        } else {
                            emptyView.setVisibility(View.GONE);
                            settlementList.setVisibility(View.VISIBLE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    private void getSelectPageSettlementLists(String type) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", type);
        jsonObject.addProperty("is_real", "2");
        OkHttpUtils.getInstance().postWithMemberToken(Url.SIMULATED_LIST, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                myScrollview.onRefreshComplete();
                                if (mSettlementsList != null) {
                                    mSettlementsList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        PositionBean positionBean = new PositionBean();
                                        positionBean.order_id = jsonArray.optJSONObject(i).optString("order_id");
                                        positionBean.createtime = jsonArray.optJSONObject(i).optString("create_time");
                                        positionBean.price = jsonArray.optJSONObject(i).optString("price");
                                        positionBean.end_time = jsonArray.optJSONObject(i).optString("end_time");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.earnings= jsonArray.optJSONObject(i).optString("earnings");
                                        positionBean.start_price = jsonArray.optJSONObject(i).optString("start_price");
                                        positionBean.full_symbol= jsonArray.optJSONObject(i).optString("full_symbol");
                                        positionBean.symbol= jsonArray.optJSONObject(i).optString("symbol");
                                        positionBean.name= jsonArray.optJSONObject(i).optString("name");
                                        positionBean.number= jsonArray.optJSONObject(i).optString("number");
                                        positionBean.service_charge = jsonArray.optJSONObject(i).optString("service_charge");
                                        positionBean.mix_price = jsonArray.optJSONObject(i).optString("mix_price");
                                        positionBean.direction= jsonArray.optJSONObject(i).optString("direction");
                                        positionBean.postion = jsonArray.optJSONObject(i).optString("overnight_fee");
                                        positionBean.margin = jsonArray.optJSONObject(i).optString("margin");
                                        positionBean.total = jsonArray.optJSONObject(i).optString("total");
                                        mSettlementsList.add(positionBean);
                                    }
                                    if (settlementAdapter != null) {
                                        settlementAdapter.refresh(mSettlementsList);
                                    }

                                } else {
                                    myScrollview.onRefreshComplete();
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (mSettlementsList.size() <= 0) {
                        emptyView.setVisibility(View.VISIBLE);
                        settlementList.setVisibility(View.GONE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        settlementList.setVisibility(View.VISIBLE);
                    }
                }else {
                    myScrollview.onRefreshComplete();
                }
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
